﻿namespace CKFinder.Settings
{
    using CKFinder;
    using System;

    public class AccessControl
    {
        private string _Folder;
        public TriStateBool FileDelete;
        public TriStateBool FileRename;
        public TriStateBool FileUpload;
        public TriStateBool FileView;
        public TriStateBool FolderCreate;
        public TriStateBool FolderDelete;
        public TriStateBool FolderRename;
        public TriStateBool FolderView;
        public string ResourceType = "*";
        public string Role = "*";

        internal AccessControl()
        {
            this.Folder = "/";
            this.FolderView = 2;
            this.FolderCreate = 2;
            this.FolderRename = 2;
            this.FolderDelete = 2;
            this.FileView = 2;
            this.FileUpload = 2;
            this.FileRename = 2;
            this.FileDelete = 2;
        }

        public int GetAllowedMask()
        {
            return (((((((((this.FolderView == 1) ? 1 : 0) | ((this.FolderCreate == 1) ? 2 : 0)) | ((this.FolderRename == 1) ? 4 : 0)) | ((this.FolderDelete == 1) ? 8 : 0)) | ((this.FileView == 1) ? 0x10 : 0)) | ((this.FileUpload == 1) ? 0x20 : 0)) | ((this.FileRename == 1) ? 0x40 : 0)) | ((this.FileDelete == 1) ? 0x80 : 0));
        }

        public int GetDeniedMask()
        {
            return (((((((((this.FolderView == null) ? 1 : 0) | ((this.FolderCreate == null) ? 2 : 0)) | ((this.FolderRename == null) ? 4 : 0)) | ((this.FolderDelete == null) ? 8 : 0)) | ((this.FileView == null) ? 0x10 : 0)) | ((this.FileUpload == null) ? 0x20 : 0)) | ((this.FileRename == null) ? 0x40 : 0)) | ((this.FileDelete == null) ? 0x80 : 0));
        }

        public string GetInternalKey()
        {
            return (this.Role + "#@#" + this.ResourceType);
        }

        public string Folder
        {
            get
            {
                return this._Folder;
            }
            set
            {
                this._Folder = value;
                if (!this._Folder.StartsWith("/"))
                {
                    this._Folder = "/" + this._Folder;
                }
                if (!this._Folder.EndsWith("/"))
                {
                    this._Folder = this._Folder + "/";
                }
            }
        }
    }
}

